package com.hl.toolkit.server.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class LoadProperties {
	
	private  static String propertyFileName;
	
	
	
     private static Properties application = new Properties();  
      
    static {  
        try {  
            //PropertiesHelper.class.getResourceAsStream("env.properties"); // /com/cici/conf/env.properties  
            //ClassLoader.getSystemResourceAsStream("env.properties");  
            InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream("application.properties");  
            application.load(is);  
            is.close();  
        } catch (Exception e) {  
        	e.printStackTrace();
         }  
    }  
      
    /** 
     * 取属性值 
     * @param key 
     * @return  
     */  
    public static String getapplicationProperty(String key){  
        return application.getProperty(key);  
    }  
    
    
    private static Properties zfbinfo = new Properties();  
    
    static {  
        try {  
            //PropertiesHelper.class.getResourceAsStream("env.properties"); // /com/cici/conf/env.properties  
            //ClassLoader.getSystemResourceAsStream("env.properties");  
            InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream("zfbinfo.properties");  
            zfbinfo.load(is);  
            is.close();  
        } catch (Exception e) {  
        	e.printStackTrace();
         }  
    }  
      
    /** 
     * 取属性值 
     * @param key 
     * @return  
     */  
    public static String getzfbinfoProperty(String key){  
        return zfbinfo.getProperty(key);  
    }  
	
	
	
	
	

	/**
     * 动态获取配置文件值(不重启获取修改后的配置文件值)
     * 效率有一定影响
     * @param propertisFile  配置文件名称（ class目录）
     * @param property 需要获取的配置项的名称
     * @return
     */
    public static String loadPropertiesValue(String key){
    	Properties props = new Properties();
    	String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    	path = path.substring(1);
        propertyFileName = "application.properties";
        System.out.println("path=" + path + propertyFileName);
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(path + propertyFileName));
            props.load(in);
            String value = props.getProperty(key);
            System.out.println(key +"="+ value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    
    
    public static String loadzfbPropertiesValue(String key){
    	Properties props = new Properties();
    	String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    	path = path.substring(1);
        propertyFileName = "zfbinfo.properties";
        System.out.println("path=" + path + propertyFileName);
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(path + propertyFileName));
            props.load(in);
            String value = props.getProperty(key);
            System.out.println(key + value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
